package com.charles.seckillPlatform.config;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @author Charles-H
 * 
 * 定时器配置
 */
@Component
public class ScheduleConfig {

    public static final Logger logger = LoggerFactory.getLogger(ScheduleConfig.class);
    
    @Autowired
    private Scheduler scheduler;

    /**
     * 启动定时器
     */
    @PostConstruct
    public void startScheduler() {
        try {
            scheduler.start();
            logger.info("定时器启动成功");
        } catch (SchedulerException e) {
            logger.error("定时器启动失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 销毁定时器
     */
    @PreDestroy
    public void stopScheduler() {
        try {
            scheduler.shutdown();
            logger.info("定时器关闭成功");
        } catch (SchedulerException e) {
            logger.error("定时器关闭失败");
            throw new RuntimeException(e);
        }
    }

}
